﻿using BaoGiaVeThang.ServiceFlight;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaoGiaVeThang
{
    public class VNACrawler : AirLineCrawler
    {
        public VNACrawler(int startDay)
            : base(startDay)
        {

        }
        public override async void UpdateCheapTicketPrice()
        {
            if (domesticRoute.IsVN)
            {
                DateTime departDate = getDate();
                try
                {
                    CrawlerClient client = new CrawlerClient();
                    RootMinPrice root =await client.GetPriceInFareVietNamAirlinesAsync(1, domesticRoute.ArrivalAirport, 0, departDate, domesticRoute.DepartureAirport,
                        0, false, departDate.AddDays(10));
                    UpdateToDatabase(root, true, false, false, domesticRoute);
                }
                catch (Exception ex)
                {
                    countError++;
#if DEBUG
                    File.AppendAllText("ErrorVN.txt", "Depart=" + domesticRoute.DepartureAirport + ";Arrival=" + domesticRoute.ArrivalAirport
                       + ";Date=" + departDate + "\n");
#endif
                }
            }
            Next();
        }
        public override bool isDone()
        {
            return monthInfo.month == MonthInfo.startMonth - 1;
        }
        public override DateTime getDate()
        {
            int year = DateTime.Now.Year;
            if (DateTime.Now.Month > monthInfo.month)
            {
                year++;
            }
            return new DateTime(year, monthInfo.month, monthInfo.startDay).AddDays(3);
        }
    }
}